<template>
  <div style="margin-top: 50px"></div>
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    style="max-width: 500px"
    label-width="auto"
    class="demo-ruleForm"
  >
    <el-form-item
      label="公司名称"
      prop="companyName"
      :rule="[{ required: true, message: '请输入公司名称', trigger: 'blur' }]"
    >
      <el-input style="width: 300px" v-model="ruleForm.companyName" />
    </el-form-item>
    <el-form-item
      label="公司类型"
      prop="companyType"
      :rule="[{ required: true, message: '请选择公司类型', trigger: 'blur' }]"
    >
      <el-select style="width: 300px" v-model="ruleForm.companyType" placeholder="公司类型">
        <el-option
          v-for="item in companytypes"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="数据文件" prop="datafile">
      <el-upload
        style="width: 300px"
        class="upload-demo"
        drag
        :limit="1"
        action="/bbb/upload"
        :on-change="uploadData"
      >
        <el-icon class="el-icon--upload">
          <upload-filled />
        </el-icon>
        <div class="el-upload__text">拖拽 或 <em>点击</em> 上传</div>
        <template #tip>
          <div class="el-upload__tip">请将碳核算数据、数据证明及碳核算结果打包后上传</div>
        </template>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type UploadFile, type UploadFiles } from 'element-plus'
import { UploadFilled } from '@element-plus/icons-vue'
import { CompanyControllerService, CompanyUserControllerService, DataReviewControllerService } from '../../../generated'

const companytypes = [
  { label: '发电企业', value: '发电企业' },
  { label: '电网企业', value: '电网企业' },
  { label: '电解铝企业', value: '电解铝企业' },
  { label: '民航企业', value: '民航企业' }
]
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref({
  companyName: '',
  companyType: '',
  url: ''
})

const uploadData = async (res: any) => {
  ruleForm.value.url = res.response.data
}

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      const res = await CompanyUserControllerService.getCompanyUsingGet()
      const data = {
        url:ruleForm.value.url,
        ossStatus:0,
        companyId:res.data?.id
      }
      const res2 = await DataReviewControllerService.addDataReviewUsingPost(data)
      if(res2.code===0){
        ElMessage({
          message: '碳核算数据上传成功',
          type: 'success'
        })
      }else{
        ElMessage({
          message: '碳核算上传失败',
          type: 'error'
        })
      }
    } else {

      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped>
.demo-ruleForm {
  margin: 50px auto;
}
</style>
